﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Snowdreamist.Text.UBB
{
    public enum ImageVisible
    {
        Default,
        Show,
        Hide
    }

    internal class ImageFactory : PrefixUBBElementFactory
    {
        public ImageFactory() : base(ElementPrefix) { }

        public override UBBElement Create(UBBContent ubbContent)
        {
            var matchResult = ElementRegex.Match(ubbContent.Text);
            if (!matchResult.Success)
                return null;

            if (matchResult.Groups.Count == 1)
            {
                return new ImageElement();
            }
            else
            {
                int value = int.Parse(matchResult.Groups[1].Value.Substring(1));
                if (value == 0)
                    return new ImageElement(ImageVisible.Hide);
                else
                    return new ImageElement(ImageVisible.Show);
            }
        }

        #region private

        private readonly static string ElementPrefix = "img";

        private readonly static Regex ElementRegex = new Regex(@"img(=[01])?", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled);

        #endregion private
    }

    public class ImageElement : UBBElement
    {
        public const string Label = "img";

        /// <summary>
        /// ctor
        /// </summary>
        public ImageElement() : this(ImageVisible.Default) { }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="visible"></param>
        public ImageElement(ImageVisible visible)
            : base(Label)
        {
            this.Visible = visible;
        }

        /// <summary>
        /// Image visible
        /// </summary>
        public ImageVisible Visible { get; private set; }

        public override string SerializeToUBBCode()
        {
            return Label + "=" + (this.Visible == ImageVisible.Show ? "1" : "0");
        }

        public override object Clone()
        {
            return new ImageElement(this.Visible);
        }
    }
}